var tabid = "";
var parenttab = null;

function settabid(id) {
    tabid = id;
}

function setParentTab(ptab) {
    parenttab = ptab;
}

function getParentTab() {
    return parenttab;
}

function getTabTitle(tabid) {
    return window.parent.$('#close' + tabid).parent().find('.title').text();
}

function setTabTitle(tabid, text) {
    window.parent.$('#close' + tabid).parent().find('.title').text(text);
}

function closetab(noevent, targetTabid) {
    window.parent.closetabbytabid(targetTabid || tabid, noevent);
}

function renewTab(tabid){
    window.top.renewTab(tabid);
}

function addtab(tabid, tabname, taburl, onloadcb) {
    window.parent.addtabbytabinfo(tabid, tabname, taburl, onloadcb, window);
}

function gettabfunction(tabid, functionname) {
    var tabwindow = window.parent.gettabframewindow(tabid);
    if (tabwindow)
        return tabwindow[functionname];
    else
        return null;
}



function framePopup() {
  var mainCss = $(".js-main").attr("class");
  $(".f-frame-btn").click(function () {
    if ($(".js-sidebar").is(":hidden")) {
      $(this).parent().attr("class", mainCss);
      $(".js-sidebar").show();
      $(this).removeClass("f-frame-btn-crt");
      $("iframe").width($(".js-main").width());
    } else {
      $(".js-sidebar").hide();
      $(this).addClass("f-frame-btn-crt");
      $(this).parent().attr("class", "col-sm-12 js-main");
      $("iframe").width($(".js-main").width());
    }
  });
}

function addtab(menuitem, tabid, tabname, taburl, onloadcb, parenttab) {
	console.info(document.getElementById('tabframe' + tabid));
  var iframeWindow = document.getElementById('tabframe' + tabid) ? document.getElementById('tabframe' + tabid).contentWindow:null;
  if(!iframeWindow || iframeWindow.my.public.setting.addTabType === 'renew')
  {
    if ($('#close' + tabid).length > 0) {
      removetab($('#close' + tabid));
    }

    if (menuitem) {
      var tabinfo = $(menuitem);
      tabname = tabinfo.text();
      taburl = tabinfo.attr('data-url');
    }

    var li = $('<li role="presentation"><a href="#tab' + tabid + '" role="tab" data-toggle="tab" title="'+tabname+'"><span class="title">' + tabname + '</span>&nbsp;<span class="glyphicon glyphicon-remove" aria-hidden="true" onclick="removetab(this, true)" id="close' + tabid + '"></span></a></li>');
    var tabpanel = $('<div role="tabpanel" class="tab-pane" id="tab' + tabid + '"><div class="panel panel-default s-1mt"><div class="panel-body s-pd0"><iframe id="tabframe' + tabid + '" src="' + taburl + '?r=' + Math.random() + '" style="border: none;width: 100%;height: 100%;"></iframe></div></div></div>');
    $('#tabpanels').append(tabpanel);
    $('#tabs').append(li);
    li.find('a').tab('show');

    resizeframe();

    document.getElementById('tabframe' + tabid).onload = function () {
      this.contentWindow.settabid('' + tabid);
      this.contentWindow.setParentTab(parenttab);

      if (typeof(onloadcb) === 'function')
        onloadcb(this.contentWindow);
    }
  }
  else{
    $('#tabs li a[href=#tab'+tabid+']').tab('show');
  }
}

function gettabframewindow(tabid) {
  return $("#tabframe" + tabid)[0].contentWindow;
}

function removetab(val,noevent){
  var tabid = $(val).attr('id').replace('close','');
  var iframeWindow = document.getElementById('tabframe' + tabid).contentWindow;
  if(noevent != true && typeof(iframeWindow.my.public.setting.beforeClose) === 'function')
  {
    iframeWindow.my.public.setting.beforeClose(tabid,removetabbytabid);
  }
  else
    removetabimp(val);
}

function renewTab(tabid) {
  setTimeout(function() {
    $('#tabs li a[href=#tab' + tabid + ']').tab('show');
    $('#tabframe' + tabid).prop('contentWindow').location.reload();
  }, 0);
}

function removetabbytabid(tabid){
  removetabimp($('#close' + tabid)[0]);
}

function removetabimp(val) {
  var panelid = $(val).parent().attr('href');
  var activetab = $(val).parent().parent().prev().find('a');
  var actived = $(val).parent().parent().hasClass('active');
  $(val).parent().parent().remove();
  $(panelid).remove();

  if (actived) {
    activetab.tab('show');
  }
}

function closetabbytabid(tabid,noevent) {
  if (tabid) {
    removetab($('#close' + tabid)[0],noevent);
  }
}

function setTabClosable(tabid,closable){
  if(closable){
    $('#close' + tabid).show();
  }else{
    $('#close' + tabid).hide();
  }
}

function addtabbytabinfo(tabid, tabname, taburl, onloadcb, parenttab) {
  if (tabid && tabname && taburl) {
    addtab(null, tabid, tabname, taburl, onloadcb, parenttab);
  }
}

function LogOff(){
  window.location.href = '/';
}

function resizeframe() {
  var h = $(window).height();
  var w = $(".js-main").width();
  $('iframe').height(h - 115);
  $('iframe').width(w);
  // $('iframe').height(document.documentElement.clientHeight);
}

window.onresize = resizeframe;

$(function () {
  resizeframe();
  framePopup();
});